package com.ujs.edu.medicine.dao;

import com.ujs.edu.medicine.dataobject.CollectUS;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
@Mapper
public interface CollectUSMapper {
    @Delete({
        "delete from collect_u_s",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    /*通过UID删除*/
    @Delete({
            "delete from collect_u_s",
            "where uId = #{uId,jdbcType=INTEGER}"
    })
    int deleteByUID(Integer uId);

    @Insert({
        "insert into collect_u_s (id, shopId, ",
        "uId)",
        "values (#{id,jdbcType=INTEGER}, #{shopid,jdbcType=INTEGER}, ",
        "#{uid,jdbcType=INTEGER})"
    })
    int insert(CollectUS record);

    @Select({
        "select",
        "id, shopId, uId",
        "from collect_u_s",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="shopId", property="shopid", jdbcType=JdbcType.INTEGER),
        @Result(column="uId", property="uid", jdbcType=JdbcType.INTEGER)
    })
    CollectUS selectByPrimaryKey(Integer id);

    @Select( {
        "select",
        "id, shopId, uId",
        "from collect_u_s"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="shopId", property="shopid", jdbcType=JdbcType.INTEGER),
        @Result(column="uId", property="uid", jdbcType=JdbcType.INTEGER)
    })
    List<CollectUS> selectAll();

    /*更具UID，shopID查询收藏信息*/
    @Select("select * from collect_u_s where uId=#{uId} and shopId=#{shopId}")
    @ResultMap("collectShopMap")
    CollectUS selectByUId(@Param("uId")Integer uId,@Param("shopId")Integer shopId);



    /*更具UID查询收藏shop信息*/
    @Select("select * from collect_u_s where uId=#{uId}")
    @Results(id="collectShopMap",value ={
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="shopId", property="shopid", jdbcType=JdbcType.INTEGER),
            @Result(column="uId", property="uid", jdbcType=JdbcType.INTEGER),
            @Result(property = "shop",column = "shopId",many = @Many(select = "com.ujs.edu.medicine.dao.YunjiayiShopMapper.selectByPrimaryKey",fetchType = FetchType.LAZY))
    })

    List<CollectUS> getShopsByuId(@Param("uId")Integer uId);

    /*获取用户收藏的药店数量*/
    @Select("select count(*) from collect_u_s where uId=#{uId}")
    Integer selectCollectShopNum(@Param("uId")Integer uId);


    @Update({
        "update collect_u_s",
        "set shopId = #{shopid,jdbcType=INTEGER},",
          "uId = #{uid,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(CollectUS record);
}